package com.lhy.vaccine_stock.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lhy.vaccine_stock.entity.SysRegion;
import com.lhy.vaccine_stock.entity.VaccineStock;
import com.lhy.vaccine_stock.form.SearchForm;
import com.lhy.vaccine_stock.service.ISysRegionService;
import com.lhy.vaccine_stock.service.impl.VaccineStockService;
import com.lhy.vaccine_stock.vo.PageVo;
import com.lhy.vaccine_stock.vo.R;
import com.lhy.vaccine_stock.vo.VaccineOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhy
 * @since 2023-02-22
 */
@RestController
@RequestMapping("/stock/vaccineStock")
public class VaccineStockController {

    @Autowired
    VaccineStockService vaccineStockService;

    @Autowired
    ISysRegionService sysRegionService;

    /**
     * 主页检索 分页
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/pageList/{currentPage}/{pageSize}")
    public R getVaccineList(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("pageSize") Integer pageSize
    ){
        PageVo<VaccineStock> pageVo=vaccineStockService.listByPage(currentPage,pageSize);
        return R.ok().put("data",pageVo);
    }

    /**
     * 搜索框检索 分页
     * @param searchForm
     * @return
     */
    @PostMapping("/search")
    public R getSelectByLike(@RequestBody SearchForm searchForm){
        PageVo<SearchForm> pageVo = vaccineStockService.search(searchForm);

        return R.ok().put("data",pageVo);
    }

    @GetMapping("/getList")
    public R getList(){
        R r = vaccineStockService.getVaccineList();
        return r;
    }

    /**
     * 用户界面 获取疫苗列表
     * @return
     */
    @GetMapping("/getUserList")
    public R getUserVaccineList(){
        R r = vaccineStockService.getVaccineList();
        return r;
    }

    @GetMapping("/getNumber")
    public R getNumber(String newValue){
        R r = vaccineStockService.getVaccineNumber(newValue);
        return r;
    }

    @GetMapping("/getVaccineById")
    public R getVaccineById(Integer vaccineId){
        VaccineStock service = vaccineStockService.getById(vaccineId);
        if (service!=null){
            return R.ok().put("data",service.getVName());
        }
        return R.error();
    }

    @GetMapping("/getVaccineByName")
    public R getVaccineByName(@RequestParam("vname")String vaccineName){
        QueryWrapper<VaccineStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("v_name",vaccineName).gt("v_stock",0);

        List<VaccineStock> vaccineStocks = vaccineStockService.list(queryWrapper);

        if (vaccineStocks.size() >0){
            return R.ok().put("vid", vaccineStocks.get(0).getVId()).put("vmsg",vaccineStocks.get(0).getVMsg());
        }
        return R.error();
    }

    /**
     * 以下 均与地区有关---------------------------------------------------------------------------------------
     * @return
     */
    @GetMapping("/getProvince")
    public R getProvince(){
        R r = sysRegionService.getProvince();
        return r;
    }
    @GetMapping("/getCity")
    public R getCity(String newValue){
        R r = sysRegionService.getCity(newValue);
        return r;
    }
    @GetMapping("/getRegion")
    public R getRegion(String newValue){
        R r = sysRegionService.getRegion(newValue);
        return r;
    }

    @PostMapping("/getVaccineId")
    public R getVaccineId(@RequestBody VaccineOrderVo vaccineOrderVo){
        R result = vaccineStockService.getVaccineId(vaccineOrderVo);

        return result;
    }

    @GetMapping("/getCityById")
    public R getCityById(String cityId){
        SysRegion sysRegion = sysRegionService.getById(cityId);
        if (sysRegion!=null){
            String name = sysRegion.getRegionName();
            return R.ok().put("data",name);
        }
        return R.error();
    }
}

